package com.example.a001.part18;



import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.a001.R;

public class LoginActivity extends AppCompatActivity implements View.OnClickListener {

    private EditText etUsername, etPassword;
    private Button btnAnonymous, btnExternal, btnInterface, btnLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login88);

        etUsername = findViewById(R.id.et_username);
        etPassword = findViewById(R.id.et_password);
        btnAnonymous = findViewById(R.id.btn_anonymous);
        btnExternal = findViewById(R.id.btn_external);
        btnInterface = findViewById(R.id.btn_interface);

        // 匿名内部类方式
        btnAnonymous.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String username = etUsername.getText().toString();
                String password = etPassword.getText().toString();
                showLoginToast(username, password);
            }
        });

        // 外部类方式
        ExternalClickListener externalClickListener = new ExternalClickListener(this, etUsername, etPassword);
        btnExternal.setOnClickListener(externalClickListener);

        // 接口实现方式
        btnInterface.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.btn_interface) {
            String username = etUsername.getText().toString();
            String password = etPassword.getText().toString();
            showLoginToast(username, password);
        }
    }

    // 布局绑定方式
    public void onLayoutButtonClick(View v) {
        String username = etUsername.getText().toString();
        String password = etPassword.getText().toString();
        showLoginToast(username, password);
    }

    private void showLoginToast(String username, String password) {
        Toast.makeText(this, "用户名: " + username + ", 密码: " + password, Toast.LENGTH_SHORT).show();
    }
}